package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.entity.Option;
import com.jyy.prefabricated.service.OptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;


/**
 * 题目选项
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class OptionController {

    @Autowired
    private OptionService optionService;

    /**
     * 新增选项
     * @param option
     * @return
     */
    @PostMapping("/options")
    public AjaxResult insert(@Valid @RequestBody Option option){
        log.info("Rest request to insert option:{}",option);
        Integer id = optionService.insert(option);
        Option result = optionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新选项
     * @param option
     * @return
     */
    @PutMapping("/options")
    public AjaxResult update(@Valid @RequestBody Option option){
        log.info("Rest request to update option:{}",option);
        optionService.update(option);
        Option result = optionService.selectById(option.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询选项详情
     * @param id
     * @return
     */
    @GetMapping("/options/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find option by id:{}",id);
        Option result = optionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除选项
     * @param id
     * @return
     */
    @DeleteMapping("/options/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete option by id:{}",id);
        int row = optionService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }


}
